]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
shell: Make singletons owned by ShellGlobal
authorJonas Ådahl <jadahl@gmail.com>
Fri, 24 Mar 2023 09:16:07 +0000 (10:16 +0100)
committerJonas Ådahl <jadahl@gmail.com>
Thu, 20 Apr 2023 12:49:38 +0000 (14:49 +0200)
This means also means they will be cleaned up when disposing
ShellGlobal, which will then mean signals tied to the GObject lifetime
will be disconnected.

Closes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/6536
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2722>

src/shell-app-cache.c
src/shell-app-system.c
src/shell-app-usage.c
src/shell-global-private.h
src/shell-global.c
src/shell-global.h
src/shell-window-tracker.c

index 44fc8b0d72dacacb64446c658d28a3e39f66349d..ef10754517b431a5cc0a989cfca1d7414e27eef5 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "shell-app-cache-private.h"
 
+#include "shell-global-private.h"
+
 /**
  * SECTION:shell-app-cache
  * @title: ShellAppCache
@@ -82,15 +84,7 @@ cache_state_new (void)
 ShellAppCache *
 shell_app_cache_get_default (void)
 {
-  static ShellAppCache *instance;
-
-  if (instance == NULL)
-    {
-      instance = g_object_new (SHELL_TYPE_APP_CACHE, NULL);
-      g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *)&instance);
-    }
-
-  return instance;
+  return shell_global_get_app_cache (shell_global_get ());
 }
 
 static void
index a5b76362deb149419c1d3f7a32710367a410be3d..f9d74ed5caccab4652b3262ce2c0cc01affce280 100644 (file)
@@ -308,12 +308,7 @@ shell_app_system_finalize (GObject *object)
 ShellAppSystem *
 shell_app_system_get_default (void)
 {
-  static ShellAppSystem *instance = NULL;
-
-  if (instance == NULL)
-    instance = g_object_new (SHELL_TYPE_APP_SYSTEM, NULL);
-
-  return instance;
+  return shell_global_get_app_system (shell_global_get ());
 }
 
 /**
index 8cbebed88af9f8f4787a8f5299b78e64c927c474..aaaabd9892633798e1ddf4282609930f53896f0b 100644 (file)
@@ -763,10 +763,5 @@ on_enable_monitoring_key_changed (GSettings     *settings,
 ShellAppUsage *
 shell_app_usage_get_default (void)
 {
-  static ShellAppUsage *instance;
-
-  if (instance == NULL)
-    instance = g_object_new (SHELL_TYPE_APP_USAGE, NULL);
-
-  return instance;
+  return shell_global_get_app_usage (shell_global_get ());
 }
index a0834af8e28d33d5f4fbef910395bd91bc884537..6132500605a6703ef00feeb62d86fbe31aff49f7 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <gjs/gjs.h>
 
+#include "shell-app-cache-private.h"
+
 void _shell_global_init            (const char *first_property_name,
                                     ...);
 void _shell_global_set_plugin      (ShellGlobal  *global,
@@ -15,6 +17,8 @@ void        _shell_global_destroy_gjs_context (ShellGlobal  *global);
 
 GjsContext *_shell_global_get_gjs_context (ShellGlobal  *global);
 
+ShellAppCache * shell_global_get_app_cache (ShellGlobal *global);
+
 gboolean _shell_global_check_xdnd_event (ShellGlobal  *global,
                                          XEvent       *xev);
 
index 0f0589e2755c54f7bf1d61bab06c24773f3aa4c9..aae9469abf589c9c3d03884880df2a82d42a9e0b 100644 (file)
@@ -39,6 +39,8 @@
 #include "shell-global-private.h"
 #include "shell-perf-log.h"
 #include "shell-window-tracker.h"
+#include "shell-app-usage.h"
+#include "shell-app-cache-private.h"
 #include "shell-wm.h"
 #include "shell-util.h"
 #include "st.h"
@@ -72,6 +74,11 @@ struct _ShellGlobal {
   GFile *userdatadir_path;
   GFile *runtime_state_path;
 
+  ShellWindowTracker *window_tracker;
+  ShellAppSystem *app_system;
+  ShellAppCache *app_cache;
+  ShellAppUsage *app_usage;
+
   StFocusManager *focus_manager;
 
   guint work_count;
@@ -479,6 +486,11 @@ shell_global_finalize (GObject *object)
   g_clear_object (&global->js_context);
   g_object_unref (global->settings);
 
+  g_clear_object (&global->window_tracker);
+  g_clear_object (&global->app_system);
+  g_clear_object (&global->app_cache);
+  g_clear_object (&global->app_usage);
+
   the_object = NULL;
 
   g_cancellable_cancel (global->switcheroo_cancellable);
@@ -720,6 +732,7 @@ _shell_global_init (const char *first_property_name,
 ShellGlobal *
 shell_global_get (void)
 {
+  g_return_val_if_fail (the_object, NULL);
   return the_object;
 }
 
@@ -1915,3 +1928,63 @@ _shell_global_notify_shutdown (ShellGlobal *global)
 {
   g_signal_emit (global, shell_global_signals[SHUTDOWN], 0);
 }
+
+/**
+ * shell_global_get_window_tracker:
+ *
+ * Gets window tracker.
+ *
+ * Return value: (transfer none): the window tracker
+ */
+ShellWindowTracker *
+shell_global_get_window_tracker (ShellGlobal *global)
+{
+  if (!global->window_tracker)
+    global->window_tracker = g_object_new (SHELL_TYPE_WINDOW_TRACKER, NULL);
+  return global->window_tracker;
+}
+
+/**
+ * shell_global_get_app_system:
+ *
+ * Gets app system.
+ *
+ * Return value: (transfer none): the app system
+ */
+ShellAppSystem *
+shell_global_get_app_system (ShellGlobal *global)
+{
+  if (!global->app_system)
+    global->app_system = g_object_new (SHELL_TYPE_APP_SYSTEM, NULL);
+  return global->app_system;
+}
+
+/**
+ * shell_global_get_app_cache:
+ *
+ * Gets app cache.
+ *
+ * Return value: (transfer none): the app cache
+ */
+ShellAppCache *
+shell_global_get_app_cache (ShellGlobal *global)
+{
+  if (!global->app_cache)
+    global->app_cache = g_object_new (SHELL_TYPE_APP_CACHE, NULL);
+  return global->app_cache;
+}
+
+/**
+ * shell_global_get_app_usage:
+ *
+ * Gets app usage.
+ *
+ * Return value: (transfer none): the app usage
+ */
+ShellAppUsage *
+shell_global_get_app_usage (ShellGlobal *global)
+{
+  if (!global->app_usage)
+    global->app_usage = g_object_new (SHELL_TYPE_APP_USAGE, NULL);
+  return global->app_usage;
+}
index beeabdeff477bbedb01147f9c8891fd9f1889b79..8399330236e16a0476ed1890fb370be52ee14c29 100644 (file)
@@ -9,6 +9,10 @@
 
 G_BEGIN_DECLS
 
+#include "shell-window-tracker.h"
+#include "shell-app-system.h"
+#include "shell-app-usage.h"
+
 #define SHELL_TYPE_GLOBAL (shell_global_get_type ())
 G_DECLARE_FINAL_TYPE (ShellGlobal, shell_global, SHELL, GLOBAL, GObject)
 
@@ -89,6 +93,12 @@ GVariant * shell_global_get_persistent_state    (ShellGlobal  *global,
                                                  const char   *property_type,
                                                  const char   *property_name);
 
+ShellWindowTracker * shell_global_get_window_tracker (ShellGlobal *global);
+
+ShellAppSystem *     shell_global_get_app_system     (ShellGlobal *global);
+
+ShellAppUsage *      shell_global_get_app_usage      (ShellGlobal *global);
+
 G_END_DECLS
 
 #endif /* __SHELL_GLOBAL_H__ */
index 50ab8f1da86b9982b2db363af677441fb5eab19a..6a7240e47438fe3e844005d7ab281786b25392b5 100644 (file)
@@ -824,10 +824,5 @@ shell_startup_sequence_get_app (MetaStartupSequence *sequence)
 ShellWindowTracker *
 shell_window_tracker_get_default (void)
 {
-  static ShellWindowTracker *instance;
-
-  if (instance == NULL)
-    instance = g_object_new (SHELL_TYPE_WINDOW_TRACKER, NULL);
-
-  return instance;
+  return shell_global_get_window_tracker (shell_global_get ());
 }